import { request } from '@/service/request';

/**
 * 医院服务
 */
export class HospitalService {
    /**
     * 获取医院列表
     */
    static async getHospitalList(pid: string = "0") {
        try {
            const response = await request.get<any>('/nurse_hospital_dept_bed/nurseHospitalDeptBed/rootList', { pid }, {
                useErrMsg: true
            });
            
            if (response.error) {
                // console.error('获取医院列表失败:', response.error);
                return { error: response.error, success: null };
            }
            
            return { error: null, success: response.success };
        } catch (error) {
            // console.error('获取医院列表失败:', error);
            return { error, success: null };
        }
    }

    /**
     * 根据区域获取医院列表
     */
    static async getHospitalListByArea(area: string) {
        try {
            const response = await request.get<any>('/nurse_hospital_dept_bed/nurseHospitalDeptBed/getHospitalListByArea', { area }, {
                useErrMsg: true
            });
            
            if (response.error) {
                // console.error('根据区域获取医院列表失败:', response.error);
                return { error: response.error, success: null };
            }
            
            return { error: null, success: response.success };
        } catch (error) {
            // console.error('根据区域获取医院列表失败:', error);
            return { error, success: null };
        }
    }

    /**
     * 编辑默认医院
     */
    static async editDefaultHospital(data: { userId: string; defaultHospital: string; defaultHospitalName: string }) {
        try {
            const response = await request.post<any>('/nurse_user/nurseUser/editDefaultHospital', data, {
                useErrMsg: true
            });
            
            if (response.error) {
                // console.error('编辑默认医院失败:', response.error);
                return { error: response.error, success: null };
            }
            
            return { error: null, success: response.success };
        } catch (error) {
            // console.error('编辑默认医院失败:', error);
            return { error, success: null };
        }
    }
}

/**
 * 医院 Hook
 */
export function use_hospital_dept_bed() {
    const getHospitalList = async (pid: string = "0") => {
        return await HospitalService.getHospitalList(pid);
    };

    const getHospitalListByArea = async (area: string) => {
        return await HospitalService.getHospitalListByArea(area);
    };

    return {
        getHospitalList,
        getHospitalListByArea
    };
}

/**
 * 用户医院操作 Hook
 */
export function useNurseUser() {
    const editDefaultHospital = async (data: { userId: string; defaultHospital: string; defaultHospitalName: string }) => {
        return await HospitalService.editDefaultHospital(data);
    };

    return {
        editDefaultHospital
    };
}
